#include<iostream>
using namespace std;
class MyMatrix {
public:
	int** data;
	int n;
	MyMatrix(int** a, int _n) {
		n = _n;
		data = new int* [n];
		for (int i = 0; i < n; i++) {
			data[i] = new int[n];
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				data[i][j] = a[i][j];
			}
		}
	}
	friend void operator *(MyMatrix& m1, MyMatrix& m2) {
		int** a = new int* [m1.n];
		for (int i = 0; i < m1.n; i++) {
			a[i] = new int[m1.n];
		}
		for (int i = 0; i < m1.n; i++) {
			for (int j = 0; j < m1.n; j++) {
				a[i][j] = 0;
				for (int k = 0; k < m1.n; k++) {
					a[i][j] += m1.data[i][k] * m2.data[k][j];
				}
			}
		}
		for (int i = 0; i < m1.n; i++) {
			for (int j = 0; j < m1.n; j++) {
				m1.data[i][j] = a[i][j];
			}
		}
	}
	void show() {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				if (j != n - 1) {
					cout << data[i][j] << ' ';
				}
				else {
					cout << data[i][j];
				}
			}
			if (i != n - 1) {
				cout << endl;
			}
		}
	}
	~MyMatrix() {
		for (int i = 0; i < n; i++) {
			delete[] data[i];
		}
		delete[] data;
	}
};
int main()
{
	int t, n;
	cin >> t >> n;
	int** a = new int* [n];
	for (int i = 0; i < n; i++) {
		a[i] = new int[n];
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			cin >> a[i][j];
		}
	}
	t--;
	MyMatrix m(a, n);
	for (int i = 0; i < t; i++) {
		int** b = new int* [n];
		for (int i = 0; i < n; i++) {
			b[i] = new int[n];
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				cin >> b[i][j];
			}
		}
		MyMatrix m1(b, n);
		m* m1;
		for (int i = 0; i < n; i++) {
			delete[] b[i];
		}
		delete[] b;
	}
	m.show();
	for (int i = 0; i < n; i++) {
		delete[] a[i];
	}
	delete[] a;
	return 0;
}